package com.hostmanagement.login;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import com.hostmanagement.user.UserBean;
import com.hostmanagement.util.db.DBUtil;
import com.hostmanagement.util.encrypt.StringUtils;

public class LoginBean {

	public UserBean getUser(String userName, String userPass) {

		ResultSet rs = null;
		Connection con = null;
		Statement stmt = null;
		
		UserBean userBean = new UserBean();
		try {
			String sql = "SELECT user_id, user_name, user_password, reg_time, rule_id, user_status, dog_id FROM user_info WHERE user_name='" + userName + "';";
			con = DBUtil.getConnection();
			stmt = con.createStatement();
			rs = stmt.executeQuery(sql);
			String password = null;
			while (rs.next()) {
				password = rs.getString("user_password");
				userBean.setUserId(rs.getInt("user_id"));
				userBean.setUserName(rs.getString("user_name"));
				userBean.setRegTime(rs.getDate("reg_time"));
				userBean.setRuleId(rs.getInt("rule_id"));
				userBean.setUserStatus(rs.getString("user_status"));
				userBean.setDogId(rs.getString("dog_id"));
			}			
			
			if (userPass != null && userPass.equals(StringUtils.decrypt(password))) {
				return userBean;
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (stmt != null)
					stmt.close();
				if (con != null)
					con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return null;
	}

}
